package com.example.famhealing.schedule;

import java.util.ArrayList;

import com.example.famhealing.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SetSideEffects extends Activity {

	MyCustomAdapter dataAdapter = null;

	public static String sideEffects = "";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sideeffects);

		displayListView();

		checkButtonClick();

	}

	private void displayListView() {

		//부작용 arraylist
		ArrayList<Sideeffects> sideeffectsList = new ArrayList<Sideeffects>();

		Sideeffects _sideeffects = new Sideeffects("구역, 구토", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("설사, 복통", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("두통", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("어지러움, 환각증세", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("변비", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("가슴 통증", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("출혈, 멍", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("대소변이 검거나 붉은 색", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("관절통, 근육통", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("부종, 얼굴이 붓는다", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("경련, 발작", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("불면증", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("탈수증", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("호흡곤란", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("고열(38도 이상)", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("발한", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("손발저림, 따끔거림, 감각이상", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("두드러기, 피부 과민반응", false);
		sideeffectsList.add(_sideeffects);
		_sideeffects = new Sideeffects("눈물, 눈부심", false);
		sideeffectsList.add(_sideeffects);

		// create an ArrayAdaptar from the String Array
		dataAdapter = new MyCustomAdapter(this, R.layout.sideeffects_info, sideeffectsList);
		ListView listView = (ListView) findViewById(R.id.listView1);
  
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
    
				// When clicked, show a toast with the TextView text
				Sideeffects sideeffects = (Sideeffects) parent.getItemAtPosition(position);
				Toast.makeText(getApplicationContext(),
						"Clicked on : " + sideeffects.getName(), Toast.LENGTH_LONG)
						.show();
			}
		});
	}

	private class MyCustomAdapter extends ArrayAdapter<Sideeffects> {

		private ArrayList<Sideeffects> sideeffectsList;

		public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<Sideeffects> stateList) {
			
			super(context, textViewResourceId, stateList);
			this.sideeffectsList = new ArrayList<Sideeffects>();
			this.sideeffectsList.addAll(stateList);
		}

		private class ViewHolder {
			TextView tv;
			CheckBox cb;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;

			Log.v("ConvertView", String.valueOf(position));

			if (convertView == null) {

				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				convertView = vi.inflate(R.layout.sideeffects_info, null);

				holder = new ViewHolder();
				holder.tv = (TextView) convertView.findViewById(R.id.code);
				holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);

				convertView.setTag(holder);

				holder.cb.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						CheckBox cb = (CheckBox) v;
						Sideeffects _sideeffects = (Sideeffects) cb.getTag();

						Toast.makeText(getApplicationContext(),
								"Checkbox: " + _sideeffects.getName() + " -> " + cb.isChecked()
								, Toast.LENGTH_LONG)
								.show();

						_sideeffects.setSelected(cb.isChecked());
					}	
				});

			} else {
				
				holder = (ViewHolder) convertView.getTag();
			}

			Sideeffects sideeffects = sideeffectsList.get(position);

			holder.tv.setText(sideeffects.getName());
			
			//holder.name.setText(state.getName());
			holder.cb.setChecked(sideeffects.isSelected());

			holder.cb.setTag(sideeffects);

			return convertView;
		}

	}

	private void checkButtonClick() {

		Button myButton = (Button) findViewById(R.id.findSelected);

		myButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				StringBuffer responseText = new StringBuffer();
				responseText.append("Selected Countries are...\n");

				ArrayList<Sideeffects> sideeffectsList = dataAdapter.sideeffectsList;

				for (int i = 0; i < sideeffectsList.size(); i++) {
					
					Sideeffects state = sideeffectsList.get(i);

					if (state.isSelected()) {
						responseText.append("\n" + state.getName());
					}
				}

				Toast.makeText(getApplicationContext(), responseText, Toast.LENGTH_LONG).show();
			}
		});
	}

}